<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="现金余额" prop="cash">
        <el-input
          v-model="queryParams.moneyMIN"
          placeholder="请输入MoneyMIN现金余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <el-input
          v-model="queryParams.moneyMAX"
          placeholder="请输入MoneyMAX现金余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.e_wallet_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:wallet:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:wallet:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:wallet:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:wallet:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="walletList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="用户" align="center" prop="user.userName"/>
      <el-table-column label="现金余额" align="center" prop="cash"/>
      <el-table-column label="总收入" align="center" prop="income"/>
      <el-table-column label="总支出" align="center" prop="expenditure"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.e_wallet_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column width="520px" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state === 1"
            size="mini"
            type="text"
            @click="congzhi(scope.row.userId)"
            v-hasPermi="['business:wallet:edit']"
          >充值
          </el-button>
          <el-button
            v-if="scope.row.state === 1"
            size="mini"
            type="text"
            @click="xiaofei(scope.row.userId)"
            v-hasPermi="['business:wallet:edit']"
          >消费
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="dongjie(scope.row.id)"
            v-hasPermi="['business:wallet:remove']"
          >冻结
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:wallet:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改钱包管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="formList" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="userId">
          <el-select v-model="formList.userId" placeholder="请选择用户" clearable>
            <el-option
              v-for="dict in users"
              :label="dict.nickName"
              :value="dict.userId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改钱包管理对话框 -->
    <el-dialog :title="title" :visible.sync="open_top_up" width="500px" append-to-body>
      <el-form ref="form" :model="formDate" :rules="rules" label-width="80px">
        <el-form-item label="充值" prop="cash">
          <el-input v-model="formDate.cash" placeholder="请输入充值金额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formDate.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getchaozhuo">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改钱包管理对话框 -->
    <el-dialog :title="title" :visible.sync="open_consume" width="500px" append-to-body>
      <el-form ref="form" :model="formDate" :rules="rules" label-width="80px">
        <el-form-item label="消费" prop="cash">
          <el-input v-model="formDate.cash" placeholder="请输入消费金额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formDate.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="getxiaofei">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWallet, getWallet, delWallet, addWallet, updateWallet, listUser,
  dongjieWallet
} from '@/api/business/wallet'
import { congzhi, xiaofei } from '@/api/business/detail'

export default {
  name: 'Wallet',
  dicts: ['e_wallet_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钱包管理表格数据
      walletList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      open_top_up: false,
      open_consume: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cash: null,
        state: null,
        remark: null,
        MoneyMIN: null,
        MoneyMAX: null
      },
      // 表单参数
      form: {},
      formList: {},
      formDate: {},
      // 表单校验
      rules: {},
      users: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询钱包管理列表 */
    getList() {
      this.loading = true
      listWallet(this.queryParams).then(response => {
        this.walletList = response.rows
        console.log(this.walletList)
        this.total = response.total
        this.loading = false
      })
    },
    getListUser() {
      this.loading = true
      listUser().then(res => {
        this.users = res.data
        console.log(this.users, '用户')
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.open_top_up = false
      this.open_consume = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formDate = {
        userId: null,
        cash: null,
        remark: null
      }
      this.form = {
        id: null,
        userId: null,
        cash: null,
        income: null,
        expenditure: null,
        state: null
      }
      this.queryParams = {
        MoneyMIN: null,
        MoneyMAX: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.queryParams = {
        MoneyMIN: null,
        MoneyMAX: null
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加钱包管理'
      this.getListUser()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getWallet(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改钱包管理'
      })
    },
    /** 新增成功 */
    submitForm() {
      addWallet(this.formList.userId).then(response => {
        this.$modal.msgSuccess('新增成功')
        this.open = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除钱包管理编号为"' + ids + '"的数据项？').then(function() {
        return delWallet(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    //充值
    congzhi(userId) {
      this.reset()
      this.formDate.userId = userId
      console.log('充值', userId)
      this.open_top_up = true
      this.title = '充值管理'
    },

    getchaozhuo() {
      congzhi(this.formDate).then(res => {
        this.$modal.msgSuccess('充值成功')
        this.open_top_up = false
        this.getList()
      })
    },
    //消费
    xiaofei(userId) {
      this.reset()
      this.formDate.userId = userId
      console.log('消费', userId)
      this.open_consume = true
      this.title = '消费管理'
    },
    getxiaofei() {
      xiaofei(this.formDate).then(res => {
        this.$modal.msgSuccess('消费成功')
        this.open_consume = false
        this.getList()
      })
    },
    //冻结
    dongjie(id) {
      this.$modal.confirm('是否确认冻结钱包管理编号为"' + id + '"的数据项？').then(function() {
        return dongjieWallet(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('冻结成功')
      }).catch(() => {
      })
      console.log('冻结', id)

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/wallet/export', {
        ...this.queryParams
      }, `wallet_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
